package com.jsh.erp.service.qoute;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.jsh.erp.datasource.entities.*;
import com.jsh.erp.datasource.mappers.*;
import com.jsh.erp.datasource.vo.TreeNode;
import com.jsh.erp.service.material.MaterialService;
import com.jsh.erp.service.materialExtend.MaterialExtendService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class QuoteService {
    @Resource
    private QuoteTemplateMapper quoteTemplateMapper;
    @Resource
    private QuoteTemplateItemMapper quoteTemplateItemMapper;
    @Resource
    private QuoteCategoryMapper quoteCategoryMapper;
    @Resource
    private MaterialMapperEx materialMapperEx;
    @Resource
    private MaterialMapper materialMapper;
    @Resource
    private MaterialService materialService;
    @Resource
    private MaterialExtendService materialExtendService;
    @Resource
    private QuoteProjectMapper quoteProjectMapper;
    @Resource
    private QuoteProjectSubsystemMapper quoteProjectSubsystemMapper;
    @Resource
    private QuoteProjectItemMapper quoteProjectItemMapper;

    public QuoteCategory categoryGetInfo(Long id){
        return quoteCategoryMapper.selectByPrimaryKey(id);
    }
    public List<QuoteCategory> findCategoryByParentId(long parentId){
        QuoteCategoryExample example = new QuoteCategoryExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        List<QuoteCategory> quoteCategories = quoteCategoryMapper.selectByExample(example);
        return quoteCategories;
    }

    public JSONArray buildCategoryTree(long parentId){
        List<QuoteCategory> quoteCategories = findCategoryByParentId(parentId);
        QuoteCategory rootNode = quoteCategories.get(0);
        JSONObject rootJson = buildTree(rootNode);
        JSONArray arr=new JSONArray();
        arr.add(rootJson);
        return arr;
    }

    private JSONObject buildTree(QuoteCategory node) {
        // 将当前节点转为 JSON 对象
        TreeNode treeNode = new TreeNode();
        treeNode.setId(node.getId());
        treeNode.setKey(node.getId());
        treeNode.setValue(node.getId());
        treeNode.setTitle(node.getName());
        JSONObject nodeJson = (JSONObject) JSONObject.toJSON(treeNode);
        // 获取当前节点的子节点
        List<QuoteCategory> children = findCategoryByParentId(node.getId());
        // 构建子节点树
        List<JSONObject> childrenJson = Lists.newArrayList();
        for (QuoteCategory child : children) {
            childrenJson.add(buildTree(child));
        }

        // 添加子节点到 JSON 对象
        nodeJson.put("children", childrenJson);

        return nodeJson;
    }

    public List<QuoteTemplate> quoteTemplates(Long categoryId,String name){
        QuoteTemplateExample example = new QuoteTemplateExample();
        example.createCriteria().andCategoryIdEqualTo(categoryId);
        List<QuoteTemplate> quoteTemplates = quoteTemplateMapper.selectByExample(example);
        return quoteTemplates;
    }

    public int categoryAdd(String name, Long parentId, String remark, int sort) {
        QuoteCategory quoteCategory = new QuoteCategory();
        quoteCategory.setName(name);
        quoteCategory.setParentId(parentId);
        quoteCategory.setRemark(remark);
        quoteCategory.setSort(String.valueOf(sort));
        return quoteCategoryMapper.insert(quoteCategory);
    }

    public int categoryEdit(Long id, String name, Long parentId, String remark, int sort) {
        QuoteCategory quoteCategory = quoteCategoryMapper.selectByPrimaryKey(id);
        quoteCategory.setName(name);
        quoteCategory.setParentId(parentId);
        quoteCategory.setRemark(remark);
        quoteCategory.setSort(String.valueOf(sort));
        return quoteCategoryMapper.updateByPrimaryKey(quoteCategory);
    }

    public int categoryDelete(String id) {
        return quoteCategoryMapper.deleteByPrimaryKey(Long.parseLong(id));
    }

    public int templateAdd(String name,String quoteCategoryId, String remark) {
        QuoteTemplate quoteTemplate = new QuoteTemplate();
        quoteTemplate.setName(name);
        quoteTemplate.setRemark(remark);
        quoteTemplate.setCategoryId(Long.parseLong(quoteCategoryId));
        return quoteTemplateMapper.insert(quoteTemplate);
    }

    public int templateEdit(Long id,String name,String quoteCategoryId, String remark) {
        QuoteTemplate quoteTemplate = quoteTemplateMapper.selectByPrimaryKey(id);
        quoteTemplate.setName(name);
        quoteTemplate.setRemark(remark);
        quoteTemplate.setCategoryId(Long.parseLong(quoteCategoryId));
        return quoteTemplateMapper.updateByPrimaryKey(quoteTemplate);
    }

    public int templateDelete(String id) {
        return quoteTemplateMapper.deleteByPrimaryKey(Long.parseLong(id));
    }

    public List<QuoteTemplateItem> quoteTemplateItemsByTemplateId(Long quoteTemplateId){
        QuoteTemplateItemExample example = new QuoteTemplateItemExample();
        example.createCriteria().andTemplateIdEqualTo(quoteTemplateId);
        List<QuoteTemplateItem> quoteTemplateItems = quoteTemplateItemMapper.selectByExample(example);
        for (QuoteTemplateItem quoteTemplateItem : quoteTemplateItems){
            try {
//                MaterialVo4Unit m = materialMapperEx.findById(quoteTemplateItem.getMaterialId()).get(0);
                List<MaterialVo4Unit> materialVo4Units = materialMapperEx.fullParamSearchById(quoteTemplateItem.getMaterialId());
                if ( !materialVo4Units.isEmpty() ){
                    quoteTemplateItem.setMaterialInfo(materialVo4Units.get(0));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return quoteTemplateItems;
    }

    public int templateItemEdit(Long quoteTemplateItemId, Long count) {
        QuoteTemplateItem quoteTemplateItem = quoteTemplateItemMapper.selectByPrimaryKey(quoteTemplateItemId);
        quoteTemplateItem.setCount(count);
        return quoteTemplateItemMapper.updateByPrimaryKey(quoteTemplateItem);
    }

    public int templateItemDelete(Long quoteTemplateItemId) {
        return quoteTemplateItemMapper.deleteByPrimaryKey(quoteTemplateItemId);
    }

    public int templateItemAdd(long quoteTemplateId, long materialId) {
        QuoteTemplateItem quoteTemplateItem = new QuoteTemplateItem();
        quoteTemplateItem.setTemplateId(quoteTemplateId);
        quoteTemplateItem.setMaterialId(materialId);
        quoteTemplateItem.setCount(0L);
        return quoteTemplateItemMapper.insert(quoteTemplateItem);
    }

    public List<QuoteProject> quoteList(long projectId) {
        QuoteProjectExample example = new QuoteProjectExample();
        example.createCriteria().andProjectIdEqualTo(projectId);
        return quoteProjectMapper.selectByExample(example);
    }

    public int projectAdd(String projectId, String name, String remark) {
        QuoteProject quoteProject = new QuoteProject();
        quoteProject.setProjectId(Long.parseLong(projectId));
        quoteProject.setName(name);
        quoteProject.setRemark(remark);
        return quoteProjectMapper.insert(quoteProject);
    }

    public int projectEdit(long id, String name, String remark) {
        QuoteProject quoteProject = quoteProjectMapper.selectByPrimaryKey(id);
        quoteProject.setName(name);
        quoteProject.setRemark(remark);
        return quoteProjectMapper.updateByPrimaryKey(quoteProject);
    }

    public int projectDelete(long id) {
        return quoteProjectMapper.deleteByPrimaryKey(id);
    }

    public List<QuoteProjectSubsystem> projectSubsystemList(long quoteProjectId) {
        QuoteProjectSubsystemExample example = new QuoteProjectSubsystemExample();
        example.createCriteria().andQuoteProjectIdEqualTo(quoteProjectId);
        return quoteProjectSubsystemMapper.selectByExample(example);
    }

    public int projectItemAdd(long quoteProjectId, long materialId) {
        QuoteProjectItem quoteProjectItem = new QuoteProjectItem();
        quoteProjectItem.setQuoteProjectId(quoteProjectId);
        quoteProjectItem.setMaterialId(materialId);
        quoteProjectItem.setCount(0L);
        return quoteProjectItemMapper.insert(quoteProjectItem);
    }

    public List<QuoteProjectItem> projectItemsByQuoteProjectId(long quoteProjectId) {
        QuoteProjectItemExample example = new QuoteProjectItemExample();
        example.createCriteria().andQuoteProjectIdEqualTo(quoteProjectId);
        List<QuoteProjectItem> quoteProjectItems = quoteProjectItemMapper.selectByExample(example);
        for (QuoteProjectItem quoteProjectItem : quoteProjectItems){
            try {
                List<MaterialVo4Unit> materialVo4Units = materialMapperEx.fullParamSearchById(quoteProjectItem.getMaterialId());
                if ( !materialVo4Units.isEmpty()){
                    quoteProjectItem.setMaterialInfo(materialVo4Units.get(0));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return quoteProjectItems;
    }

    public int projectItemEdit(Long id,Long count,String remark) {
        QuoteProjectItem quoteProjectItem = quoteProjectItemMapper.selectByPrimaryKey(id);
        quoteProjectItem.setCount(count);
        quoteProjectItem.setRemark(remark);
        return quoteProjectItemMapper.updateByPrimaryKey(quoteProjectItem);
    }

    public int projectItemDelete(String id) {
        return quoteProjectItemMapper.deleteByPrimaryKey(Long.parseLong(id));
    }

    public int projectItemImport(long quoteProjectId, long quoteTemplateId) {
        List<QuoteTemplateItem> quoteTemplateItemList = quoteTemplateItemsByTemplateId(quoteTemplateId);
        for (QuoteTemplateItem quoteTemplateItem : quoteTemplateItemList){
            QuoteProjectItem quoteProjectItem = new QuoteProjectItem();
            quoteProjectItem.setQuoteProjectId(quoteProjectId);
            quoteProjectItem.setMaterialId(quoteProjectItem.getMaterialId());
            quoteProjectItem.setCount(quoteTemplateItem.getCount());
            quoteProjectItemMapper.insert(quoteProjectItem);
        }
        return quoteTemplateItemList.size();
    }

    public void projectCopy(long quoteProjectId) {
        QuoteProject sourceQuoteProject = quoteProjectMapper.selectByPrimaryKey(quoteProjectId);
        QuoteProject targetQuoteProject = new QuoteProject();
        targetQuoteProject.setName("copy from "+sourceQuoteProject.getName());
        targetQuoteProject.setProjectId(sourceQuoteProject.getProjectId());
        targetQuoteProject.setRemark(sourceQuoteProject.getRemark());
        quoteProjectMapper.insert(targetQuoteProject);

        QuoteProjectExample quoteProjectExample = new QuoteProjectExample();
        quoteProjectExample.createCriteria().andNameEqualTo(targetQuoteProject.getName());
        List<QuoteProject> quoteProjects = quoteProjectMapper.selectByExample(quoteProjectExample);
        QuoteProject targetQuoteProjectEntity = null ;
        if ( quoteProjects.isEmpty() ){
            throw new RuntimeException();
        }
        targetQuoteProjectEntity = quoteProjects.get(0);

        QuoteProjectItemExample quoteProjectItemExample = new QuoteProjectItemExample();
        quoteProjectItemExample.createCriteria().andQuoteProjectIdEqualTo(sourceQuoteProject.getId());
        List<QuoteProjectItem> sourceProjectItems = quoteProjectItemMapper.selectByExample(quoteProjectItemExample);

        for (QuoteProjectItem sourceQuoteProjectItem : sourceProjectItems){
            QuoteProjectItem targetQuoteProjectItem = new QuoteProjectItem();
            targetQuoteProjectItem.setQuoteProjectId(targetQuoteProjectEntity.getId());
            targetQuoteProjectItem.setMaterialId(sourceQuoteProjectItem.getMaterialId());
            targetQuoteProjectItem.setCount(sourceQuoteProjectItem.getCount());
            quoteProjectItemMapper.insert(targetQuoteProjectItem);
        }

    }
}
